package com.bootdo.jiashan.service.impl;

import com.bootdo.common.utils.MoneyConvert;
import com.bootdo.common.utils.RejectUtil;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.jiashan.model.SupplyProductModel;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.jiashan.dao.SupplyProductDao;
import com.bootdo.jiashan.domain.SupplyProductDO;
import com.bootdo.jiashan.service.SupplyProductService;



@Service
public class SupplyProductServiceImpl implements SupplyProductService {
	@Autowired
	private SupplyProductDao supplyProductDao;

	static String[] headers ={"品名","规格","重量","价格","厂名","手机","电话","联系人","品牌"};
	
	@Override
	public SupplyProductDO get(Integer id){
		SupplyProductDO supplyProductDO =  supplyProductDao.get(id);
		supplyProductDO.setWeightStr(MoneyConvert.centToDollarForString(supplyProductDO.getWeight()));
		supplyProductDO.setUnitPriceStr(MoneyConvert.centToDollarForString(supplyProductDO.getUnitPrice()));
		supplyProductDO.setRealPriceStr(MoneyConvert.centToDollarForString(supplyProductDO.getRealPrice()));
		return supplyProductDO;
	}
	
	@Override
	public List<SupplyProductModel> list(Map<String, Object> map){
		List<SupplyProductModel>  list =  supplyProductDao.list(map);
		if(CollectionUtils.isNotEmpty(list)){
			for (SupplyProductModel supplyProductModel : list) {
				supplyProductModel.setWeightStr(MoneyConvert.centToDollarForString(supplyProductModel.getWeight()));
				supplyProductModel.setUnitPriceStr(MoneyConvert.centToDollarForString(supplyProductModel.getUnitPrice()));
				supplyProductModel.setRealPriceStr(MoneyConvert.centToDollarForString(supplyProductModel.getRealPrice()));
			}
		}
		return list;
	}

	@Override
	public int count(Map<String, Object> map){
		return supplyProductDao.count(map);
	}
	
	@Override
	public int save(SupplyProductDO supplyProduct) throws IllegalAccessException {
		if(StringUtils.isNotEmpty(supplyProduct.getWeightStr())){
			supplyProduct.setWeight(MoneyConvert.DollarToCentForLong(supplyProduct.getWeightStr()));
		}
		if(StringUtils.isNotEmpty(supplyProduct.getUnitPriceStr())){
			supplyProduct.setUnitPrice(MoneyConvert.DollarToCentForLong(supplyProduct.getUnitPriceStr()));
		}
		if(StringUtils.isNotEmpty(supplyProduct.getRealPriceStr())){
			supplyProduct.setRealPrice(MoneyConvert.DollarToCentForLong(supplyProduct.getRealPriceStr()));
		}
		RejectUtil.nullToEmptyStrOr0(supplyProduct);
		supplyProduct.setCtime(new Date());
		supplyProduct.setUtime(new Date());
		return supplyProductDao.save(supplyProduct);
	}
	
	@Override
	public int update(SupplyProductDO supplyProduct){
		if(StringUtils.isNotEmpty(supplyProduct.getWeightStr())){
			supplyProduct.setWeight(MoneyConvert.DollarToCentForLong(supplyProduct.getWeightStr()));
		}
		if(StringUtils.isNotEmpty(supplyProduct.getUnitPriceStr())){
			supplyProduct.setUnitPrice(MoneyConvert.DollarToCentForLong(supplyProduct.getUnitPriceStr()));
		}
		if(StringUtils.isNotEmpty(supplyProduct.getRealPriceStr())){
			supplyProduct.setRealPrice(MoneyConvert.DollarToCentForLong(supplyProduct.getRealPriceStr()));
		}
		supplyProduct.setUtime(new Date());
		return supplyProductDao.update(supplyProduct);
	}
	
	@Override
	public int remove(Integer id){
		return supplyProductDao.remove(id);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return supplyProductDao.batchRemove(ids);
	}

	@Override
	public XSSFWorkbook exportExcel(Map<String, Object> map) {
		List<SupplyProductModel> list = list(map);
		return exportExcel(list);
	}

	private XSSFWorkbook exportExcel(List<SupplyProductModel>  list){
		XSSFWorkbook xwk = new XSSFWorkbook();
		XSSFSheet xssfSheet = xwk.createSheet("导出结果");
		createHeader(xssfSheet);
		createContent(xssfSheet,list);
		return xwk;
	}

	private void createHeader(XSSFSheet xssfSheet){
		Row firstRow = xssfSheet.createRow(0);
		for (int i = 0; i < headers.length; i++) {
			Cell cell = firstRow.createCell(i);
			cell.setCellValue(headers[i]);
		}
	}

	private void createContent(XSSFSheet xssfSheet,List<SupplyProductModel>  list){
		if(CollectionUtils.isNotEmpty(list)){
			for (int i = 0; i < list.size(); i++) {
				SupplyProductModel model = list.get(i);
				Row row = xssfSheet.createRow(i+1);// +1跳过第一行（表头行）
				Cell proNameCell = row.createCell(0);
				proNameCell.setCellValue(model.getName());
				Cell normCell = row.createCell(1);
				normCell.setCellValue(model.getNorm());
				Cell weightCell = row.createCell(2);
				weightCell.setCellValue(model.getWeightStr());
				Cell unitPriceCell = row.createCell(3);
				unitPriceCell.setCellValue(model.getUnitPriceStr());
				Cell facCell = row.createCell(4);
				facCell.setCellValue(model.getSupplyName());
				Cell conMobileCell = row.createCell(5);
				conMobileCell.setCellValue(model.getContactMobile());
				Cell phoneCell = row.createCell(6);
				phoneCell.setCellValue(model.getPhone());
				Cell conCell = row.createCell(7);
				conCell.setCellValue(model.getContact());
				Cell brandCell = row.createCell(8);
				brandCell.setCellValue(model.getBrand());
			}
		}
	}

}
